package com.zer0.tga.entity.livingentity;

import com.zer0.tga.art.ArtArchive;
import com.zer0.tga.art.Bitmap;
import com.zer0.tga.entity.Entity;
import com.zer0.tga.entity.abstractentity.Blinker;
import com.zer0.tga.entity.abstractentity.Damager;
import com.zer0.tga.entity.abstractentity.Teleporter;
import com.zer0.tga.entity.livingentity.flyingentity.TeemoDart;
import com.zer0.tga.entity.livingentity.mob.Mob;
import com.zer0.tga.input.Input;
import com.zer0.tga.level.Level;
import com.zer0.tga.math.Coordinate;
import com.zer0.tga.math.Direction;

public class Teemo extends LivingEntity{
	
    private Input keys;
	private double movement_x;
	private double movement_y;
	
	private int fireCD=0;
	private boolean death;
	
	public int gold;

	public Teemo(Input keys, Coordinate position, Coordinate radius, Level level) {
        super(position, radius, level);
        setKeys(keys);
        
       // init(keys);
      /*  startX = x;
        startY = y;

        score = 10000;

        this.setSize(15,15);*/
    }
	
	public void init()
	{
        movement_y = 0;
        movement_x = 0;
        movementSpeed = 3.0;
        health = maxHealth = 6;
        direction = Direction.SOUTH;
        unitType = UnitType.FRIENDLY;
        damage = 0;
        death = false;
	}
	
	public void tick()
	{
	
		//System.out.println("Teemo is in: " + position.x + " " + position.y);
		livingTime++;
		

		if (hurtTime > 0) {
			hurtTime--;
			invincible = true;
			if(maxHurtTime - hurtTime > 5)
			{
				// knockback maybe?
			}
		}
		if (hurtTime == 0)
			invincible = false;
		
		
		if(fireCD>0)
			fireCD--;

		// Hop two three four...
		if(!level.isAdvancing()) // maybe add if not stunned etc?
		{
			Direction newdirection = direction;
			
			if (keys.up.isDown) {
				movement_y-=movementSpeed;
				newdirection = Direction.NORTH;
			}
			if (keys.down.isDown) {
				movement_y+=movementSpeed;
				newdirection = Direction.SOUTH;
			}
			if (keys.left.isDown) {
				movement_x-=movementSpeed;
				newdirection = Direction.WEST;
			}
			if (keys.right.isDown) {
				movement_x+=movementSpeed;
				newdirection = Direction.EAST;
			}
			
			if(movement_x!=0 || movement_y!=0)
			{
				if(movement_x == 0 && ( newdirection == Direction.NORTH || newdirection == Direction.SOUTH ) )
					direction = newdirection;
				else if(movement_y == 0 && ( newdirection == Direction.EAST || newdirection == Direction.WEST ) )
					direction = newdirection;
				
				move(movement_x,movement_y);
			}
			
			movement_x=0;
			movement_y=0;
		}
		
		
		if(keys.fire.isDown){
			if(fireCD<=0)
			{
				fireCD = 30;
				level.addEntityToCurrentRoom(new TeemoDart(position,new Coordinate(10,10),keys.getMouseInGamePosition(),level));
			}
			
		}
		if(keys.bomb.wasReleased())
		{
			level.addEntityToCurrentRoom(new Blinker(new Coordinate(position.x,position.y),new Coordinate(100,100),0xa0ff0000,10,10,10));
		}
		
		if(keys.fire.wasReleased()) // a little trick for mashers =D
		{
			//System.out.println("blablabal");
			fireCD-=5;
		}
		
	//	System.out.println("status:" + health + "/" + maxHealth);
		
		if(!death && health<=0)
		{
			die();
		}
	}
	


	 public void setKeys(Input keys)
	 {
		 this.keys = keys;
	 }
	

	@Override
	public Bitmap getArt() {
		// TODO Auto-generated method stub
		if(invincible && (livingTime%16 < 9))
			return ArtArchive.Teemo[0][direction.getDirection()-1].inColor(0x80fdfdfd);
		else
			return ArtArchive.Teemo[0][direction.getDirection()-1];
		}

	@Override
	public boolean collision(Entity entity, Direction dir) {
		// TODO Auto-generated method stub
		if(entity instanceof Teleporter)
			((Teleporter) entity).teleport(this,dir);
		else if(entity instanceof Damager )
		{
			hurt(((Damager) entity).damage);
			return true;
		}
		else if(entity instanceof Mob)
		{
			hurt(((Mob) entity).damage);
			return true;
		
		}
		return false;
		
	}
	
	public void die()
	{
		death = true;
		System.out.println("Game over");
		level.gameover();
	}

	@Override
	public void action() {
		// TODO Auto-generated method stub
		
	}



	


}
